<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleDialogClose"
    :lock-scroll="false"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    :class="{'mobile-el-dialog': showMobilePage}"
    class="subcribe-common-dialog small-common-dialog-size"
  >
    <DialogTitle title="安全值" font-size="16px" style="margin-bottom:20px;"/>
    <div class="flex-center">
      <div class="input-label"><span v-if="!showMobilePage" class="require-icon">* </span>安全值：</div>
      <div class="input-box">
        <el-input v-model="securityVal" clearable size="small" placeholder="请输入" @input="valToPureNum" />
        <div class="input-tip">建议值：近30天该商品出库数量为{{ stockOutQuantity }}</div>
      </div>
    </div>
    <div class="dialog-footer">
      <el-button :disabled="btnDisabled" class="ok" type="primary" @click="handleConfirm">确定</el-button>
      <el-button class="cancel-btn-style" type="info" @click="handleDialogClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import DialogTitle from '@/components/Title';
import { mapState } from 'vuex';

export default {
  components: {
    DialogTitle
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String | Number,
      default: null
    },
    stockOutQuantity: {
      type: String | Number,
      default: null
    }
  },
  data() {
    return {
      dialogVisible: false
    };
  },
  computed: {
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    }),
    securityVal: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('change', val);
      }
    },
    btnDisabled() {
      return !(this.securityVal || this.securityVal === 0);
    }
  },
  methods: {
    valToPureNum(val) {
      if (val) {
        val = val.replace(/[^\d]/g, '');
        val = val.replace(/^0[^]+/g, '0');
      }
      val = Number(val) > 999999999 ? 999999999 : val;
      this.$emit('change', val);
    },
    handleDialogClose() {
      this.dialogVisible = false;
    },
    handleConfirm() {
      this.$emit('confirm');
      this.handleDialogClose();
    }
  }
};
</script>

<style lang="scss" scoped>
.input-label {
  margin-right: 12px;
  line-height: 32px;
  color: #666;
  align-self: self-start;
  .require-icon {
    color: red;
  }
}
.input-box {
  width: 240px;
  ::v-deep .el-input__inner {
    height: 32px;
    line-height: 32px;
    width: 240px;
    font-size: 14px;
  }
  .input-tip {
    margin-top: 4px;
    font-size: 12px;
    color: #999;
  }
}
.dialog-footer {
  display: flex;
  justify-content: center;
  margin-top: 25px;
  .ok {
    width: 120px;
    font-size: 16px;
    padding: 8px 20px;
  }
  .cancel-btn-style {
    font-size: 14px;
    padding: 8px 20px;
    margin-left: 16px !important;
  }
}
</style>
